package com.walker.elk.netty.protobuf;

import com.walker.elk.netty.protobuf.server.ProtoBufServerChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class WalkerProtoBufServer {
	
	private int port;
	
	public WalkerProtoBufServer() {
		super();
	}

	public WalkerProtoBufServer(int port) {
		super();
		this.port = port;
	}
	
	public void start() {
		
		EventLoopGroup boosGroup = new NioEventLoopGroup();
		EventLoopGroup workGroup = new NioEventLoopGroup();
		
		ServerBootstrap bootstrap = new ServerBootstrap();
		bootstrap.group(boosGroup, workGroup).channel(NioServerSocketChannel.class).
		handler(new LoggingHandler(LogLevel.INFO)).
		childHandler(new ProtoBufServerChannelInitializer());
		
		try {
			ChannelFuture channelFuture = bootstrap.bind(port).sync();
			channelFuture.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			workGroup.shutdownGracefully();
			boosGroup.shutdownGracefully();
		}
		
	}

	public static void main(String[] args) {
		WalkerProtoBufServer heartbeatServer = new WalkerProtoBufServer(8891);
		heartbeatServer.start();
	}

}
